﻿using System;

public static class Converter
{
    public static string ToHex(Int32 decValue)
    {
        /*!
        Procedure Name: ToHex
        Description: (overloaded) Converts a decimal value to a hexadecimal value
        Input Parameters: Int32
        Output Parameters: string (hexadecimal)
        Errors Conditions Tested:
        Error Messages Generated:
        Original Author: Greg Loesch
        Procedure Creation Date: 11/18/2009
        Who: SH, RM
        When: 11/22/2009
        Why: Changed to the "non-hacked" version of hex conversion
        Who: SH
        When: 11/24/2009
        Why: Uppercased the output
        Coding standards met:
        Testing standards met:
        !*/
        return Convert.ToString(decValue, 16).ToUpper().PadLeft(6,'0');
    }
    public static string ToHex(Int64 decValue)
    {
        /*!
        Procedure Name: ToHex
        Description: (overloaded) Converts a decimal value to a hexadecimal value
        Input Parameters: Int64
        Output Parameters: string (hexadecimal)
        Errors Conditions Tested:
        Error Messages Generated:
        Original Author: Ryan McDaniel
        Procedure Creation Date: 11/21/2009
        Who: SH, RM
        When: 11/22/2009
        Why: Changed to the "non-hacked" version of hex conversion
        Who: SH
        When: 11/24/2009
        Why: Uppercased the output
        Coding standards met:
        Testing standards met:
        !*/
        return Convert.ToString(decValue, 16).ToUpper().PadLeft(6,'0') ;
    }

    public static string ToHex(string binValue)
    {
        /*!
        Procedure Name: ToHex
        Description: (overloaded) Converts a binary value to a hexadecimal value
        Input Parameters: string (binary)
        Output Parameters: string (hexadecimal)
        Errors Conditions Tested:
        Error Messages Generated:
        Original Author: Greg Loesch
        Procedure Creation Date: 11/18/2009
        Modification Log:
        Who: SH, RM
        When: 11/22/2009
        Why: Changed to the "non-hacked" version of hex conversion
        Who: SH
        When: 11/24/2009
        Why: Uppercased the output
        Coding standards met:
        Testing standards met:
        !*/
        binValue = binValue.PadLeft(24, '0');
        return Convert.ToString(Convert.ToInt32(binValue, 2), 16).ToUpper().PadLeft(6,'0');
    }

    public static string ToBin(string hexValue)
    {
        /*!
        Procedure Name: ToBin
        Description: (overloaded) Converts a hexadecimal value to a binary value
        Input Parameters: string hexValue
        Output Parameters: string (in binary)
        Errors Conditions Tested:
        Error Messages Generated:
        Original Author: Greg Loesch
        Procedure Creation Date: 11/18/2009
        Modification Log:
        Who: SH
        When: 11/24/2009
        Why: Uppercased the output
        Who: Greg Loesch
        When: 11/29/2009
        Why: Moved the ToUpper and PadLeft to try block.
        Coding standards met:
        Testing standards met:
        !*/
        string binaryString;
        hexValue = hexValue.PadLeft(6, '0');
        try
        {
            binaryString = Convert.ToString(Convert.ToInt32(hexValue, 16), 2);
            binaryString = binaryString.ToUpper().PadLeft(24, '0');
        }
        catch
        {
            binaryString = "";
        }
        return binaryString;
    }

    public static string ToBin(Int32 decValue)
    {
        /*!
        Procedure Name: ToBin
        Description: (Overloaded) Converts a decimal value to a binary value
        Input Parameters: Int32
        Output Parameters: string (in binary) or empty string if it fails
        Errors Conditions Tested:
        Error Messages Generated:
        Original Author: Greg Loesch
        Procedure Creation Date: 11/18/2009
        Modification Log:
        Who: Greg Loesch
        When: Returns an empty string if the parse fails.
        Why: This provides an easy way to check errors in syntax.
        Who: SH
        When: 11/24/2009
        Why: Uppercased the output
        Coding standards met:
        Testing standards met:
        !*/
        string binaryString;

        try
        {
            binaryString = Convert.ToString(decValue, 2);
        }
        catch
        {
            binaryString = "";
        }
        return binaryString.ToUpper().PadLeft(24,'0');
    }

    public static int ToInt(string hexValue,bool twosComp)
    {
        /*!
        Procedure Name: ToInt
        Description: (Copied from Tressel-Intermediate) Converts a hexadecimal string to an integer. 
                    IMPORTANT: if a hex string is improperly formed, a -1 is returned. 
                    This will only be a valid check if the twosComp is false!
        Input Parameters: string, bool (if twosComp).
        Output Parameters: integer
        Errors Conditions Tested:
        Error Messages Generated:
        Original Author: Ryan McDaniel
        Procedure Creation Date: 10/6/2009
        Modification Log:
        Who: Steve Rudy
        When: 10/11/2009
        Why: I've moved this method from the Form class to the TresselIntermediate class, and
        to make it easier to user, I've made it static
        Who: Steve Rudy
        When: 11/7/2009
        Why: we needed to compensate for two's complement
        Who: Greg Loesch
        When: 11/21/2009
        Why: Added a way to determine if an error occurs. If an error occurs in parse, returns a -1.
        Who: SH
        When: 11/24/2009
        Why: Uppercased the output
        Who: People
        When: 11/29/2009
        Why: Negatives didn't work
        Coding standards met: SR
        Testing standards met: RM
        !*/

        /*#
        Variable Name: firstDigit
        Module Where Defined: ToInteger
        Data Type: int
        Local or Global: Local
        Purpose: keeps the first digit of hex in decimel
        #*/
        int firstDigit = 0;
        int returnInt = 0;
        hexValue = hexValue.PadLeft(6, '0');

        bool skipToEnd = false;
        try
        {
            firstDigit = int.Parse(hexValue[0].ToString(), System.Globalization.NumberStyles.HexNumber);
        }
        catch
        {
            returnInt = -1;
            skipToEnd = true;
        }
        /*#
        Variable Name: returnMultiplier
        Module Where Defined: ToInteger
        Data Type: int
        Local or Global: Local
        Purpose: return negative for error checking.
        #*/
        int returnMultiplier = 1;
        if (!skipToEnd)
        {
            if (twosComp)
            {
                if (firstDigit > 7)
                {
                    hexValue = hexValue.PadLeft(8, 'F');
                    //int intValue = Convert.ToInt32(hexValue, 16);
                    //intValue *= -1;
                    //firstDigit = firstDigit - 8;
                    //hexValue = hexValue.Remove(0, 1);
                    //hexValue = hexValue.Insert(0, firstDigit.ToString());
                    //returnMultiplier = -1;
                }
            }

            try
            {
                returnInt = int.Parse(hexValue, System.Globalization.NumberStyles.HexNumber) * returnMultiplier;
            }
            catch
            {
                returnInt = -1;
            }
        }

        return returnInt;
    }


}
